package com.two.screen.water.balance.print

import com.two.screen.water.balance.listener.MyObserver
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.print.all.USBPrinterAll
import com.two.screen.water.balance.print.android_print.bluetooth.BluetoothPrinter
import com.two.screen.water.balance.print.android_print.usb.AndroidUSBPrinter
import com.two.screen.water.balance.print.auto_reply.USBPrinterAutoReply
import com.two.screen.water.balance.print.bean.PrintConnectBean
import com.two.screen.water.balance.print.bean.PrintConnectSaleReportBean
import com.two.screen.water.balance.print.bean.PrintOrderBean
import com.two.screen.water.balance.print.bean.PrintServerCollectOrderBean
import com.two.screen.water.balance.utils.*
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe

object PrinterAll {

    fun printerInit() {
        Observable.create(ObservableOnSubscribe<String> { e ->
            try {
                printerClose()
                d("DataApi.mPrinterTypeIndex:${DataApi.mPrinterTypeIndex}")
                when (DataApi.mPrinterTypeIndex) {
                    1 -> USBPrinterAutoReply.initUsbDevice()
                    2 -> USBPrinterAll.initUsbDevice()//放在AppService启动，授权窗口弹在应用应用后面。
                    3 -> AndroidUSBPrinter.initUsbDevice()//放在 login初始化，授权后重启不启动，原因AppService启动慢了。
                }
            } catch (exc: Exception) {
                d("打印初始失败:$exc")
                e.onError(exc)
            }
        })
            .compose(RxUtils.nThread())
            .subscribe(object : MyObserver<String>() {
                override fun onNext(t: String) {
                    ToastUtil.showToast(t)
                }

                override fun onError(e: Throwable) {
                    ToastUtil.showToast(e.message.toString())
                }
            })
    }

    fun isPrintConnectSuccess(): Boolean {
        return when (DataApi.mPrinterTypeIndex) {
            1 -> USBPrinterAutoReply.mIsOpen
            2 -> USBPrinterAll.mIsOpenSuccess
            3 -> AndroidUSBPrinter.mIsOpenSuccess
            else -> false
        }
    }

    fun printerTest(isBluetoothPrint: Boolean = false) {
        Observable.create(ObservableOnSubscribe<String> { e ->
            try {
                if (isBluetoothPrint) {
                    BluetoothPrinter.printTest()
                    return@ObservableOnSubscribe
                }
                when (DataApi.mPrinterTypeIndex) {
                    1 -> USBPrinterAutoReply.printTest()
                    2 -> USBPrinterAll.printTest()
                    3 -> AndroidUSBPrinter.printTest()
                }
            } catch (exc: Exception) {
                d("测试打印失败：$exc")
                e.onError(exc)
            }
        })
            .compose(RxUtils.nThread())
            .subscribe(object : MyObserver<String>() {
                override fun onNext(t: String) {
                    ToastUtil.showToast(t)
                }

                override fun onError(e: Throwable) {
                    d("测试打印：$e")
                    ToastUtil.showToast(e.message.toString())
                }
            })

    }

    fun printerClose() {
        try {
            USBPrinterAutoReply.closePrinter()
            USBPrinterAll.closePrinter()
            AndroidUSBPrinter.closePrinter()
            d("printerClose")
        } catch (e: Exception) {
            d("关闭打印失败:$e")
        }
    }

    fun d(msg: String) {
        CommUtils.println("PrinterAll->", msg)
    }

//    fun printLastOrder(mLastOrderDB: OrderDB?) {
//        Observable.create(ObservableOnSubscribe<String> { e ->
//            try {
//                USBPrinterAutoReply.printLastOrder(mLastOrderDB)
//            } catch (exc: Exception) {
//                d("测试打印失败：$exc")
//                e.onError(exc)
//            }
//        })
//            .compose(RxUtils.nThread())
//            .subscribe(object : MyObserver<String>() {
//                override fun onNext(t: String) {
//                    ToastUtil.showToast(t)
//                }
//
//                override fun onError(e: Throwable) {
//                    d("printLastOrder：$e")
//                    ToastUtil.showToast(e.message.toString())
//                }
//            })
//    }

    /*打印订单*/
    fun printOrder(data: PrintOrderBean?) {

        Observable.create(ObservableOnSubscribe<String> { e ->
            try {
                when (DataApi.mPrinterTypeIndex) {
                    1 -> USBPrinterAutoReply.printOrder(data)
                    2 -> USBPrinterAll.printOrder(data)
                    3 -> AndroidUSBPrinter.printOrder(data)
                }
                BluetoothPrinter.printOrder(data)
            } catch (exc: Exception) {
                d("测试打印失败：$exc")
                e.onError(exc)
            }
        })
            .compose(RxUtils.nThread())
            .subscribe(object : MyObserver<String>() {
                override fun onNext(t: String) {
                    ToastUtil.showToast(t)
                }

                override fun onError(e: Throwable) {
                    d("printLastOrder：$e")
                    ToastUtil.showToast(e.message.toString())
                }
            })
    }

    /*打印交接总数*/
    fun printConnectStatics(data: PrintConnectBean) {

        Observable.create(ObservableOnSubscribe<String> { e ->
            try {
                when (DataApi.mPrinterTypeIndex) {
                    1 -> USBPrinterAutoReply.printConnectStatics(data)
                    2 -> USBPrinterAll.printConnectStatics(data)
                    3 -> AndroidUSBPrinter.printConnectStatics(data)
                }
                BluetoothPrinter.printConnectStatics(data)
            } catch (exc: Exception) {
                d("测试打印失败：$exc")
                e.onError(exc)
            }
        })
            .compose(RxUtils.nThread())
            .subscribe(object : MyObserver<String>() {
                override fun onNext(t: String) {
                    ToastUtil.showToast(t)
                }

                override fun onError(e: Throwable) {
                    d("printLastOrder：$e")
                    ToastUtil.showToast(e.message.toString())
                }
            })
    }

    /*打印交接 销售商品报表*/
    fun printConnectSaleReport(data: PrintConnectSaleReportBean?) {

        Observable.create(ObservableOnSubscribe<String> { e ->
            try {
                when (DataApi.mPrinterTypeIndex) {
                    1 -> USBPrinterAutoReply.printConnectSaleReport(data)
                    2 -> USBPrinterAll.printConnectSaleReport(data)
                    3 -> AndroidUSBPrinter.printConnectSaleReport(data)
                }
                BluetoothPrinter.printConnectSaleReport(data)
            } catch (exc: Exception) {
                d("测试打印失败：$exc")
                e.onError(exc)
            }
        })
            .compose(RxUtils.nThread())
            .subscribe(object : MyObserver<String>() {
                override fun onNext(t: String) {
                    ToastUtil.showToast(t)
                }

                override fun onError(e: Throwable) {
                    d("printLastOrder：$e")
                    ToastUtil.showToast(e.message.toString())
                }
            })
    }

    fun printServerCollectOrder(printBean: PrintServerCollectOrderBean) {
        Observable.create(ObservableOnSubscribe<String> { e ->
            try {
                when (DataApi.mPrinterTypeIndex) {
                    1 -> USBPrinterAutoReply.printServerCollectOrder(printBean)
                    2 -> USBPrinterAll.printServerCollectOrder(printBean)
                    3 -> AndroidUSBPrinter.printServerCollectOrder(printBean)
                }
                BluetoothPrinter.printServerCollectOrder(printBean)
            } catch (exc: Exception) {
                d("测试打印失败：$exc")
                e.onError(exc)
            }
        })
            .compose(RxUtils.nThread())
            .subscribe(object : MyObserver<String>() {
                override fun onNext(t: String) {
                    ToastUtil.showToast(t)
                }

                override fun onError(e: Throwable) {
                    d("printLastOrder：$e")
                    ToastUtil.showToast(e.message.toString())
                }
            })
    }
}